/*
 * Copyright (C) 2008,2009,2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#include "exl.h"

/*
 * Initialization
 */
static ehash_t *ht_strings;

/*
 * Implementation
 */
void
init_string(void)
{
    enew_hash((eobject_t *)&ht_strings, H_string, 97);
    eadd_root((eobject_t *)&ht_strings);
}

evector_t *
eget_string(euint8_t *name, eint32_t length)
{
    eentry_t	*entry;
    evector_t	*string;

    if ((entry = eget_hash_string(ht_strings,
				  (euint8_t *)name, length)) == null) {
	v_check(1);
	evector(&string, t_const|t_uint8, length, 1);
	v_push(string);
	memcpy(string->v.u8, name, length);
	enew(&entry, entry);
	entry->name = string->v.u8;
	entry->value = string;
	eput_hash(ht_strings, entry);
	v_dec();
    }
    else
	string = entry->value;

    return (string);
}

evector_t *
enew_string(euint8_t *name)
{
    eentry_t	*entry;
    evector_t	*string;

    if ((entry = eget_hash(ht_strings, name)) == null)
	string = eget_string(name, eobject_length(name));
    else
	string = entry->value;

    return (string);
}
